package com.piece.admin.controller.system;

import com.piece.admin.model.TbSmsTemplate;
import com.piece.admin.service.TbSmsTemplateService;
import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.web.controller.FrameWorkController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = {"短信模板配置管理"})
@RequestMapping("/api/sms/template")
public class SmsTemplateController extends FrameWorkController {

    @Resource
    private TbSmsTemplateService tbSmsTemplateService;

    @PostMapping("/list")
    @AclPermission(code = "sms:template:view")
    @ApiOperation(value = "查询短信模板配置列表")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public List list() {
        QueryTable queryTable = initTable();
        DataTable dataTable = tbSmsTemplateService.findPage(queryTable);
        return dataTable.getData();
    }

    @PostMapping("/table")
    @AclPermission(code = "sms:template:view")
    @ApiOperation(value = "查询短信模板配置分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse table() {
        QueryTable queryTable = initTable();
        DataTable dataTable = tbSmsTemplateService.findPage(queryTable);
        return success(dataTable);
    }

    @GetMapping("/load")
    @AclPermission(code = "sms:template:view")
    @ApiOperation(value = "加载短信模板配置表单数据")
    @ApiImplicitParam(name="id", value = "主键")
    public AjaxResponse load() {
        long id = Convert.toLong(getRequest().getParameter("id"), 0L);
        TbSmsTemplate template = new TbSmsTemplate();
        if (id != 0) {
            template = tbSmsTemplateService.findById(id);
        }
        return success(template);
    }

    @SubmitPermission
    @PostMapping("/save")
    @AclPermission(code = "sms:template:add,sms:template:edit")
    @ApiOperation(value = "保存短信模板配置表单数据")
    @ApiImplicitParam(value = "短信模板配置数据", dataTypeClass = TbSmsTemplate.class, type = "body")
    public AjaxResponse save() {
        TbSmsTemplate template;
        try {
            Long id = Convert.toLong(getRequest().getParameter("id"));
            if (null != id) {
                template = tbSmsTemplateService.findById(id);
            } else {
                template = new TbSmsTemplate();
            }
            initDB(template);
            tbSmsTemplateService.insertOrUpdate(template);
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete/{id}")
    @AclPermission(code = "sms:template:delete")
    @ApiOperation(value = "删除短信模板配置数据")
    @ApiImplicitParam(name = "id", value = "主键", type = "path")
    public AjaxResponse delete(@PathVariable String id) throws Exception {
        tbSmsTemplateService.delete(id);
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "sms:template:delete")
    @ApiOperation(value = "删除短信模板配置数据")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse deleteBatch() throws Exception {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            tbSmsTemplateService.delete(Convert.toLongArray(ids));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/send")
    @AclPermission(code = "sms:template:add")
    @ApiOperation(value = "发送测试短信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value="接收手机号"),
            @ApiImplicitParam(value = "短信模板配置数据", dataTypeClass = TbSmsTemplate.class, type = "body")
    })
    public AjaxResponse send() {
        String mobile = getRequest().getParameter("mobile");
        try {
            TbSmsTemplate template = new TbSmsTemplate();
            initDB(template);
            tbSmsTemplateService.send(mobile, template);
            return success();
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
